package com.lai.shop.service.impl;



import cn.hutool.core.util.ObjectUtil;
import com.alibaba.cloud.context.utils.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lai.shop.dao.CategoryDao;
import com.lai.shop.entity.CategoryEntity;
import com.lai.shop.service.CategoryService;
import com.lai.shop.utils.PageUtils;
import com.lai.shop.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.lai.shop.dao.ProductDao;
import com.lai.shop.entity.ProductEntity;
import com.lai.shop.service.ProductService;


@Service("productService")
public class ProductServiceImpl extends ServiceImpl<ProductDao, ProductEntity> implements ProductService {


    @Autowired
    CategoryDao categoryDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        String catId = (String) params.get("categoryId");
        String isDisable = (String) params.get("isDisable");
        String max = (String) params.get("max");
        String min = (String) params.get("min");

        QueryWrapper<ProductEntity> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)){
            queryWrapper.eq("id",key)
                    .or().like("product_name",key);
        }

        if(!StringUtils.isEmpty(isDisable)){
            queryWrapper.eq("is_disable",Integer.parseInt(isDisable));
        }
        //le: 小于
        if(!StringUtils.isEmpty(max) && !"0".equalsIgnoreCase(max)){
            queryWrapper.le("product_price",max);
        }
        //ge： 大于
        if(!StringUtils.isEmpty(min) && !"0".equalsIgnoreCase(min)){
            queryWrapper.ge("product_price",min);
        }

        if(!StringUtils.isEmpty(catId) && !"0".equalsIgnoreCase(catId)){
            Integer categoryId = Integer.parseInt(catId);
            if(categoryId != 0){
                queryWrapper.eq("category_id",categoryId);
                IPage<ProductEntity> page = this.page(
                        new Query<ProductEntity>().getPage(params),
                        queryWrapper
                );
                return new PageUtils(page);
            }
        }



        IPage<ProductEntity> page = this.page(
                new Query<ProductEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);


    }

    @Override
    public List<ProductEntity> getFlowerList(String type,String category) {

        //List<ProductEntity> productEntityList = baseMapper.selectListByCategoryType(type,category);

        return null;
    }

    @Override
    public PageUtils getProductList(Map<String, Object> params) {
        /*String type = (String) params.get("type");
        String current = (String) params.get("page");
        String limit = (String) params.get("limit");
        QueryWrapper<ProductEntity> queryWrapper = new QueryWrapper<>();

        baseMapper.selectListByCategoryType(type,null,new Page<>(current,limit));

         Page<> tPage = new Page<>(page, limit);
        QueryWrapper<ProductEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.
        IPage<ProductEntity> page = this.page(
                new Query<ProductEntity>().getPage(params),

        );
        String type = (String) params.get("type");
        String category = null;
        String cate = (String) params.get("category");
        if(!StringUtils.isEmpty(cate)){
            category = cate;
        }*/
        /*String current = (String) params.get("page");
        //int currenti = Integer.parseInt(current);
        params.put("page",current);

        String limits = (String) params.get("limit");
        //int limit = Integer.parseInt(current);
        params.put("limit",limits);*/


        CategoryEntity categoryEntity = categoryDao.selectOne(new QueryWrapper<CategoryEntity>().eq("category_name", (String) params.get("categoryName")));
        QueryWrapper<ProductEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id",categoryEntity.getId());
        IPage<ProductEntity> page = this.page(
                new Query<ProductEntity>().getPage(params),queryWrapper
                );

        return new PageUtils(page);
    }

    @Override
    public ProductEntity getGoodsDetailById(Long id) {
        ProductEntity productEntity = baseMapper.selectById(id);
        return productEntity;
    }

}
